//#include "io_ctrl.h"
#include <stdint.h>

__asm uint32_t i2s_fetch_32bit_64clk(void)
{
	CPSID	i
    PUSH	{r4-r7}
	LDR		r0, =0x20148000
    LDR		r1, =0x00000010 //clk-4 hi mask
	MVNS	r2, r1			//clk-4 lo mask
//	LDR		r3, =0x00010000 //ws-16 hi mask
//	MVNS	r4, r3			//ws-16 lo mask
	LDR		r3, =0x00000040 //ws-6 hi mask
	MVNS	r4, r3			//ws-6 lo mask
    
	LDR		r5, [r0,#0]
	ANDS	r5, r5, r4	//ws > low
	STR		r5, [r0,#0]
	NOP
	NOP
	EORS	r7, r7, r7	//data reg clear
	//for beg
	EORS	r6, r6, r6
FOR_BEG_0
	ORRS	r5, r5, r1	//clk > high
	STR		r5, [r0,#0]
	ANDS	r5, r5, r2	//clk > low
	NOP
	NOP
	//NOP
	STR		r5, [r0,#0]
	ADDS	r6, r6, #1
///////////////// Latch beg //////////////////
	LDR		r4, [r0,#0x0]
    NOP
	LDR		r4, [r0,#0x50] //read pin
	//LSRS	r4, r4, #22
	//LSLS	r4, r4, #31
	//LSRS	r4, r4, #31
	LSLS	r4, r4, #31-5 //data-5
	LSRS	r4, r4, #31
	LSLS	r7, r7, #1
	ORRS	r7, r7, r4
///////////////// Latch end //////////////////
	CMP		r6, #0x10
	BCC		FOR_BEG_0
	//for end
	ORRS	r5, r5, r3	//ws > high
	STR		r5, [r0,#0]
	NOP
	//for beg
	EORS	r6, r6, r6
FOR_BEG_1
	ORRS	r5, r5, r1	//clk > high
	STR		r5, [r0,#0]
	ANDS	r5, r5, r2	//clk > low
	NOP
	NOP
	//NOP
	STR		r5, [r0,#0]
	ADDS	r6, r6, #1
	NOP
	CMP		r6, #0x10
	BCC		FOR_BEG_1
	MOV		r0, r7
    POP	{r4-r7}
	CPSIE	i
	BX		lr
}

